﻿using System;
using System.Collections;
using System.Reflection;
using log4net;
using log4net.Config;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.ServiceModel;
using System.ComponentModel;
using System.ServiceModel.Security;
using System.Collections.Generic;
using System.ServiceModel.Description;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;


namespace HaiFang
{
    [Serializable]
    public class ProxyUserRights
    {
        private static string PROCEDURE_QueryUserRights = "QueryUserRights";
        private static string PROCEDURE_InsertUserRights = "InsertUserRights";

        //@CurrUserID			int = null,					--管理员编号
        //@ProjectID			bigint,
        //@UserID				bigint	
        public static IList<UserRights> QueryUserRights(int CurrUserID, int ProjectID, int UserID)
        {
            IList<UserRights> list = new List<UserRights>();
            try
            {
                using (SqlHelper helper = new SqlHelper())
                {
                    #region 构造参数
                    string comandText = PROCEDURE_QueryUserRights;
                    List<SqlParameter> Parameters = new List<SqlParameter>();
                    Parameters.Add((new SqlParameter("@CurrUserID", CurrUserID)));
                    Parameters.Add(new SqlParameter("@ProjectID", ProjectID));
                    Parameters.Add(new SqlParameter("@UserID", UserID));
                    #endregion

                    #region 获取执行结果
                    DataTable dt = helper.ExecuteDataTable(comandText, CommandType.StoredProcedure, Parameters);
                    list = helper.GetObjects<UserRights>(dt);
                    #endregion
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            return list;
        }

        /// <summary>
        /// 转换成权限树的形式
        /// </summary>
        /// <param name="CurrUserID"></param>
        /// <param name="ProjectID"></param>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public static IList<UserRightsTree> QueryUserRightsTree(int CurrUserID, int ProjectID, int UserID)
        {
            IList<UserRightsTree> list = new List<UserRightsTree>();
            try
            {
                IList<UserRights> ur = QueryUserRights(CurrUserID, ProjectID,UserID);

                Dictionary<int, UserRightsTree> dic = new Dictionary<int, UserRightsTree>();
                if (ur != null && ur.Count > 0)
                {
                    for (int i = 0; i < ur.Count; i++)
                    {
                        if (dic.ContainsKey(ur[i].RightFlag))
                        {
                            UserRightsTree urt=dic[ur[i].RightFlag];
                            urt.UserRights.Add(ur[i]);
                        }
                        else
                        {
                            UserRightsTree urt=new UserRightsTree();
                            urt.RightFlag=ur[i].RightFlag;
                            urt.FlagName=ur[i].FlagName;
                            urt.UserRights=new List<UserRights>();
                            urt.UserRights.Add(ur[i]);
                            dic.Add(urt.RightFlag, urt);
                        }
                    }
                }
                foreach (KeyValuePair<int, UserRightsTree> r in dic)
                {
                    list.Add(r.Value);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            return list;
        }

        //@CurrUserID			int = null,					--管理员编号
        //@UserID				bigint=0,
        //@RightIDStr			varchar(8000)				--RightFlag和RightID之间以，号进行分割，串与串之间以；号分割
        public static void InsertUserRights(int CurrUserID, int UserID, string RightIDStr)
        {
            try
            {
                using (SqlHelper helper = new SqlHelper())
                {
                    #region 构造参数
                    string comandText = PROCEDURE_InsertUserRights;
                    List<SqlParameter> Parameters = new List<SqlParameter>();
                    Parameters.Add((new SqlParameter("@CurrUserID", CurrUserID)));
                    Parameters.Add(new SqlParameter("@UserID", UserID));
                    Parameters.Add(new SqlParameter("@RightIDStr", RightIDStr));
                    #endregion

                    #region 获取执行结果
                    helper.Execute(comandText, CommandType.StoredProcedure, Parameters);
                    #endregion
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
        }
    }

    [Serializable]
    public class UserRights
    {
        public int RightFlag { get; set; }
        public string FlagName { get; set; }
        public int RightID { get; set; }
        public string RightName { get; set; }
        public int SelectFlag { get; set; }
    }

    [Serializable]
    public class UserRightsTree
    {
        public int RightFlag { get; set; }
        public string FlagName { get; set; }
        public IList<UserRights> UserRights { get; set; }
    }

    [Serializable]
    public class UserRightsWithUser
    {
        public UserInfoWithGroup UserInfoWithGroup { get; set; }
        public IList<UserRights> UserRights { get; set; }
    }
}
